<!-- 列表项 :求购 -->
<template>
	<div class="item hand " @click="publishByMe?go2DemandEditDetail(item):go2DemandDetail(item)">
		<div class="tag tag1">{{$t('demand-buy')}}</div>
		<div class="item_left">
			<div class="avatarBox" v-if="!publishByMe">
				<div class="avatar">
					<img :src="item.avatar?item.avatar:avatar" alt="" class="img">
				</div>
				<div>
					<p class="name">{{item.nickname}}</p>
				</div>
			</div>
			<div class="avatarBox" v-else>
				<div class="avatar">
					<img :src="userInfo.avatar" alt="" class="img">
				</div>
				<div>
					<p class="name">{{userInfo.nickname}}</p>
				</div>
			</div>

		</div>
		<div class="item_cen">
			<!-- 中间左侧 -->
			<div class="item_cen_l">
				<!-- 描述 -->
				<p class="item_cen_name mb">{{item.title}} <img src="@/assets/img/icon_new.svg" alt=""
						v-if="isNew(item.createTime)" class="img"></p>
				<!-- 地域要求 -->
				<p class="item_region">{{$t('demand-label-regionalReq')}}{{item.region}}</p>
				<!-- 品牌要求 -->
				<p class="item_txt">
					{{$t('demand-label-brandReq')}}{{item.brandName}}
				</p>
				<!-- 需求时间 -->
				<p class="item_time">{{$t('demand-label-demandTime')}}{{item.demandTime}}</p>
			</div>
			<!-- 中间右侧 -->
			<div class="item_cen_r">
				<!-- 数量 -->
				<p class="item_num">{{$t('demand-label-production')}}{{item.demandNum}}</p>
			</div>
		</div>
		<!-- 按钮 -->
		<div class="item_right">
			<template v-if="!item.isMe && !fromCenter">
				<span class="btn btn-disabled" v-if="isCollect">{{$t('claimed')}}</span>
				<span class="btn hand" @click.stop="getCollect(item)" v-else>{{$t('claim')}}</span>
			</template>
		</div>
	</div>
</template>

<script>
	import {
		getCollect
	} from '@/api/demand'
	export default {
		props: {
			item: {
				type: Object,
				required: true
			},
			//是否来自个人中心，隐藏立即认领按钮
			fromCenter: {
				type: Boolean,
				default: false
				
			},
			//是否来自个人中心我的发布，展示我的头像昵称
			publishByMe: {
				type: Boolean,
				default: false
				
			},
		},


		computed: {
			isNew() {
				return (date) => {
					const days = 7 // 相差的天数小于这个天数为最新
					if (!date) return
					let startTime = new Date(date); // 创建时间
					let currentTime = new Date(); // 当前时间
					return (currentTime - startTime) / 1000 / 60 / 60 / 24 < days;
				}
			},
		},
		data() {
			return {
				isCollect: 0,
				avatar: require('assets/img/logo.svg')
			}
		},
		mounted() {
			this.isCollect = this.item ? this.item.isCollect : 0
			console.log()
		},
		methods: {
			// 领取
			getCollect(item) {
				if (!this.userInfo) {
					this.$modal.confirm(this.$t("tip-login"), this.locale).then(() => {
						this.go2Login()
					})
					return;
				}
				this.invoke(getCollect, item.id).then(res => {
					if (res && res.code == 200) {
						this.$message.success(this.$t('claim-success'));
						this.isCollect = 1;
					}
				})
			},
		}
	}
</script>
<style lang="scss" scoped src="@/assets/styles/demand-item.scss"></style>